package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalMedicineSort;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 药品管理Action
 * @author kangyj
 * @datetime 2014年7月27日,上午11:12:40
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MedicineSortAction extends PublicAction implements ModelDriven<MedicalMedicineSort> {

	private static final long serialVersionUID = -4243539071649896148L;
	
	private MedicalMedicineSort medicineSort = new MedicalMedicineSort();
	
	/**
	 * @desc 判断是否有重复的药品名称
	 * @return null
	 * @author liy
	 * @datetime 2014年12月18日,下午2:42:56
	 */
	@Action(value = "medicine_sort_repeat_name")
	public String judgeRepeatName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicineSort.getName())){
			boolean result = this.baseAllService.getMedicineService().isRepeatName(medicineSort.getPid(),medicineSort.getName());
			map.put("result", result);
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 跳转到药品类别管理页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月25日,下午6:13:25
	 */
	@Action(value = "medicine_sort_init", results = {@Result(name = SUCCESS, location = "medicine_sort_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载药品类别树
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月25日,下午6:13:54
	 */
	@Action(value = "medicine_sort_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMedicineService().findMedSortList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 判断当前操作的药品类别下是否有子类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月26日,下午12:00:47
	 */
	@Action(value = "medicine_sort_judge")
	public String judgeChild(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicineSort.getId())){
			boolean result = this.baseAllService.getMedicineService().containChild(medicineSort.getId());
			if(result){
				map.put("result", "true");
			}else{
				map.put("result", "false");
			}
		}else{
			map.put("result", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 判断药品是否和药品关联
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月26日,下午11:13:36
	 */
	@Action(value = "medicine_sort_use")
	public String inUse(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicineSort.getId())){
			Boolean flag = this.baseAllService.getMedicineService().checkSortInUse(medicineSort.getId());
			if(flag) {
				map.put("result", "true");
			}else {
				map.put("result", "false");
			}
		}else {
			map.put("result", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除药品类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月26日,下午2:15:40
	 */
	@Action(value = "medicine_sort_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicineSort.getId())){
			this.baseAllService.getMedicineService().delMedicineSort(medicineSort);
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加药品类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月31日,下午2:39:42
	 */
	@Action(value = "medicine_sort_add", results = {@Result(name = SUCCESS, location = "medicine_sort_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 编辑药品类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月25日,下午6:24:16
	 */
	@Action(value = "medicine_sort_edit", results = {@Result(name = SUCCESS, location = "medicine_sort_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑药品类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年7月31日,下午2:16:15
	 */
	@Action(value = "medicine_sort_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(medicineSort.getId())){
			boolean flag = this.baseAllService.getMedicineService().saveEditMedicineSort(medicineSort);
			if(flag){
				map.put("success", "true");
			}else {
				map.put("success", "error");
			}
		}else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 保存药品类别
	 * @return null
	 * @author kangyj
	 * @datetime 2014年7月26日,下午3:40:36
	 */
	@Action(value = "medicine_sort_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag = this.baseAllService.getMedicineService().saveMedicineSort(medicineSort, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	
	public MedicalMedicineSort getMedicineSort() {
		return medicineSort;
	}
	
	public void setMedicineSort(MedicalMedicineSort medicineSort) {
		this.medicineSort = medicineSort;
	}

	@Override
	public MedicalMedicineSort getModel() {
		return medicineSort;
	}

}
